<template>
	<div class="m0myheader">
		<div class="conbox">
			<div class="conboxL">
				<img src="@/assets/picture/tx.png" class="tt" />
				<div class="btR">
					<p class="p1">{{nickname}}</p>
				</div>
			</div>
			<div class="conboxR">
				<router-link to="/myziliao">
					<a>设置</a>
				</router-link>
			</div>
			<div class="clear"></div>
		</div>
		<div class="conbox2">
		</div>
	</div>
	<div class="clear"></div>
	<div class="mypart1">
		<ul>
			<li>
				<a>
					<p class="p1">{{balance}}</p>
					<p class="p2">余额</p>
				</a>
			</li>
			<li>
				<a>
					<p class="p1">{{orderNum}}</p>
					<p class="p2">订单数量</p>
				</a>
			</li>
		</ul>
	</div>
	<div class="kbox"></div>
	<div class="kbox"></div>
	<div class="clear"></div>
	<div class="mypart2">
		<div class="con">
			<div class="pa2_tit">
				<p>我的订单</p>
				<router-link to="/allDd">查看更多订单</router-link>
			</div>
		</div>
	</div>
	<Footer />
	<div v-if="tip" class="tip-layer">
		<div class="tip">{{tipContent}}</div>
	</div>
</template>

<script>
	import axios from 'axios';
	import {
		showTips
	} from '@/assets/js/common.js';
	import Footer from '@/components/footer/index.vue'
	export default {
		name: 'my',
		components: {
			Footer
		},
		props: {
			msg: String
		},
		data() {
			return {
				httpUrl: 'http://qgm1.fengjian123.com/service9501/',
				tipContent: '',
				tip: 0,
				nickname: '',
				balance: 0,
				orderNum: 0,
			};
		},
		beforeRouteEnter(to, from, next) {
			const sessionData = window.sessionStorage.getItem('users');
			if (sessionData == null) {
				next('/login');
			} else {
				next();
			}
		},
		mounted() {
			this.isLogin();
		},
		methods: {
			isLogin() {
				const sessionData = window.sessionStorage.getItem('users');
				const userData = JSON.parse(sessionData);
				if (sessionData == null) {
					this.$router.push('/login');
					console.log("登录失效");
					return;
				}

				axios.get(this.httpUrl + 'user/getUser', {
						headers: {
							'Authorization': 'Bearer ' + userData.token,
							'Content-Type': 'application/json',
						},
					})
					.then(res => {
						if (res.data.err == 0) {
							this.balance = res.data.balance;
							this.orderNum = res.data.orderNum;
							this.nickname = res.data.nickname;
						} else {
							console.error(res.data.msg);
							showTips(this, res.data.msg);
							this.$router.push('/login');
							return;
						}
					})
					.catch(error => {
						console.error("登录失败，请检查网络", error);
						showTips(this, '登录失败，请检查网络');
						this.$router.push('/login');
						return;
					});
			}
		},
	}
</script>

<style>
	@import url('@/assets/css/style.css');
</style>